package com.sds.sweng.swat.miplatform.ui.design.parts.area.sub;

import java.util.List;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.GridLayout;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;

import com.sds.sweng.swat.miplatform.ui.design.layouts.EasyGridData;
import com.sds.sweng.swat.miplatform.ui.design.layouts.EasyGridLayout;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.TableCellSubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.area.sub.TableSubArea;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MTableComponents;
import com.sds.sweng.swat.miplatform.ui.design.models.component.MTableLabel;
import com.sds.sweng.swat.miplatform.ui.design.models.component.logical.LogicalComponent;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.AreaTemplatePart;
import com.sds.sweng.swat.miplatform.ui.design.parts.area.SearchAreaTemplatePart;
import com.sds.sweng.swat.miplatform.ui.design.policies.TableCellLayoutEditPolicy;

public class TableCellSubAreaPart extends SubAreaPart {

	private static final Color searchLabelColor = new Color(null, 239, 234, 214);
	private static final Color generalLabelColor = new Color(null, 232, 239, 245);
	private static final Color labelLineColor = new Color(null, 215, 214, 167);

	public boolean isSelectable() {
		if (getModel() instanceof MTableLabel)
			return false;
		else
			return true;
	}

	private EditPart getAreaTemplatePart() {
		EditPart parentPart = getParent();

		while (!(parentPart instanceof AreaTemplatePart)) {
			parentPart = parentPart.getParent();
		}

		return parentPart;
	}

	protected IFigure createFigure() {
		Label f = new Label();
		f.setBorder(new LineBorder(labelLineColor));

		if (getModel() instanceof MTableLabel) {
			if (getAreaTemplatePart() instanceof SearchAreaTemplatePart)
				f.setBackgroundColor(searchLabelColor);
			else
				f.setBackgroundColor(generalLabelColor);

			f.setOpaque(true);
		}

		((TableSubAreaPart) getParent()).refreshAll();

		drawChildren(f);

		return f;
	}

	private void drawChildren(Label parent) {
		TableCellSubArea cell = (TableCellSubArea) getModel();

		if (cell instanceof MTableLabel) {
			MTableLabel tableLabel = (MTableLabel) cell;

			String labelText = "";
			if (tableLabel.isRequired())
				labelText = " * " + tableLabel.getText();
			else
				labelText = " " + tableLabel.getText();

			parent.setText(labelText);
		} else {
			parent.setText("");
			clearChildren(parent);

			MTableComponents tableComponents = (MTableComponents) cell;

			List<LogicalComponent> components = tableComponents.getComponents();

			GridLayout layout = EasyGridLayout.create(components.size()*2, false, MTableComponents.CELL_LEFT_MARGIN-1, MTableComponents.CELL_TOP_MARGIN, 0, 0);
			parent.setLayoutManager(layout);

			for (int i=0;i<components.size();i++) {
				LogicalComponent component = components.get(i);
				IFigure componentFigure = component.getFigure();

				int width = componentFigure.getSize().width;
				int height = componentFigure.getSize().height;

				layout.setConstraint(componentFigure, EasyGridData.create(width, height, 1, 1, SWT.BEGINNING, SWT.BEGINNING));
				parent.add(componentFigure);

				if (i < components.size()-1 && component.getRightMargin() > 0) {
					IFigure marginFigure = new Figure();
					layout.setConstraint(marginFigure, EasyGridData.create(component.getRightMargin(), 1));
					parent.add(marginFigure);
				}
			}
		}

		parent.setSize(cell.getWidth(), cell.getHeight());

		getParentLayout().setConstraint(parent, EasyGridData.create(cell.getWidth(), cell.getHeight(), cell.getColspan(), 1));
	}

	protected void createEditPolicies() {
		super.createEditPolicies();

		installEditPolicy(EditPolicy.LAYOUT_ROLE, new TableCellLayoutEditPolicy());
	}

	protected void refreshVisuals() {
		drawChildren((Label) getFigure());
		refreshCellHeight();
	}

	private void refreshCellHeight() {
		TableCellSubArea currentCell = (TableCellSubArea) getModel();
		int currentRow = currentCell.getRow();
		int newHeight = TableCellSubArea.DEFALT_HEIGHT;

		TableSubArea parentModel = (TableSubArea) ((TableSubAreaPart) getParent()).getModel();
		for (int i=0;i<parentModel.getChildren().size();i++) {
			if (parentModel.getChildren().get(i) instanceof MTableComponents) {
				MTableComponents tableComponents = (MTableComponents) parentModel.getChildren().get(i);

				if (tableComponents.getRow() == currentRow) {
					for (LogicalComponent component : tableComponents.getComponents()) {
						while (newHeight < component.getHeight()+4) {
							newHeight += TableCellSubArea.DEFALT_HEIGHT;
						}
					}
				}
			}
		}

		for (int i=0;i<getParent().getChildren().size();i++) {
			TableCellSubAreaPart tableCellPart = (TableCellSubAreaPart) getParent().getChildren().get(i);
			TableCellSubArea tableCell = (TableCellSubArea) tableCellPart.getModel();

			if (tableCell.getRow() == currentRow) {
				tableCell.setHeight(newHeight);

				IFigure tableCellFigure = tableCellPart.getFigure();
				tableCellFigure.setSize(tableCell.getWidth(), tableCell.getHeight());
				tableCellPart.setFigure(tableCellFigure);

				getParentLayout().setConstraint(tableCellFigure, EasyGridData.create(tableCell.getWidth(), tableCell.getHeight(), tableCell.getColspan(), 1));
			}
		}

		((TableSubAreaPart) getParent()).refreshAll();
	}

}
